package com.yc.util;
import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.Calendar; 
import java.util.Date; 


/** 
* 
* 1)日期，字符串，时间戳，日历之间的类型转换
* 2)取出日期的年,月,日
* 3)计算俩日期相差的时间
* 
*/ 
public class DateUtils { 
     
    private static String patter = "yyyy-MM-dd"; 
     
    private static SimpleDateFormat sdf = new SimpleDateFormat(patter); 
    /** 
     * @description:  时间模式设置
     */  
    public static void changePattern(String patter){ 
        DateUtils.patter = patter; 
        DateUtils.sdf = new SimpleDateFormat(patter); 
    } 
    
    
    /** 
     * @description:  date转换成日历
     * @param         date
     * @return        calendar
     */  
    public static Calendar convertDateToCalendar(Date date){ 
        Calendar cal = Calendar.getInstance(); 
        cal.setTime(date); 
        return cal; 
         
    } 
    /** 
     * @description:  date转换成string
     * @param         date
     * @return        string
     */   
    public static String convertDateToString(Date date){ 
        return sdf.format(date); 
    } 
    /** 
     * @description:  string转换date
     * @param         string
     * @return        date
     */       
	public static Date convertStringToDate(String sDate) {
		Date date = null;
		try {
			date = sdf.parse(sDate);
		} catch (ParseException e) {

			e.printStackTrace();
		}
		return date;
	}
    /** 
     * @description:  取出取出其中的年份
     * @param         string parse
     * @return        string year
     */ 
    public static String getYearByString(String yMdHms){ 
        Date date = convertStringToDate(yMdHms); 
        Calendar cal = convertDateToCalendar(date); 
        return String.valueOf(cal.get(Calendar.YEAR)); 
    } 
    /** 
     * @description:  取出其中的月份
     * @param         string parse
     * @return        string month
     */ 
    public static String getMonthByString(String str){ 
        Date date = convertStringToDate(str); 
        Calendar cal = convertDateToCalendar(date); 
        return String.valueOf(cal.get(Calendar.MONTH)+ 1); 
    } 
    /** 
     * @description:  取出其中的天数
     * @param         string parse
     * @return        string day
     */ 
    public static String getDayByString(String str){ 
        Date date = convertStringToDate(str); 
        Calendar cal = convertDateToCalendar(date); 
        return String.valueOf(cal.get(Calendar.DAY_OF_WEEK)); 
    } 
    /**
     * @description:  获得当前日期
     * */
    public static String getToday() { 
        Calendar cl = Calendar.getInstance(); 
        String month = ""; 
        String day = ""; 
        String today = ""; 
        String today2 = ""; 
        int m = cl.get(Calendar.MONTH) + 1; 
        if (m < 10) { 
            month = "0" + String.valueOf(m); 
        } 
        else { 
            month = String.valueOf(m); 
        } 
        int d = cl.get(Calendar.DATE); 
        if (d < 10) { 
            day = "0" + String.valueOf(d); 
        } 
        else { 
            day = String.valueOf(d); 
        } 
        today = cl.get(Calendar.YEAR) +  month + day + cl.get(Calendar.HOUR_OF_DAY) + cl.get(Calendar.MINUTE)
        + cl.get(Calendar.SECOND); 
        today2 = cl.get(Calendar.YEAR) +"-"+  month +"-"+ day +" "+ cl.get(Calendar.HOUR_OF_DAY) +":"
        + cl.get(Calendar.MINUTE)+ ":"+cl.get(Calendar.SECOND); 
        return today2; 
    } 
     
    /** 
     * @description: 返回date1与date2相差的天数 
     *  
     * @param date1 
     * @param date2 
     * @return int 
     */  
    public static int DateDiff(Date date1, Date date2) {  
        int i = (int) ((date1.getTime() - date2.getTime()) / 3600 / 24 / 1000);  
        return i;  
    }  
    
    /** 
     * @description: 返回date1与date2相差的分钟数 
     *  
     * @param date1 
     * @param date2 
     * @return int 
     */  
    public static int MinDiff(Date date1, Date date2) {  
        int i = (int) ((date1.getTime() - date2.getTime()) / 1000 / 60);  
        return i;  
    }  
    
    /** 
     * @description: 返回date1与date2相差的秒数 
     *  
     * @param date1 
     * @param date2 
     * @return int 
     */  
    public static int TimeDiff(Date date1, Date date2) {  
        int i = (int) ((date1.getTime() - date2.getTime()));  
        return i;  
    }  
    
    public static void main(String[] args) { 
        //changePattern("yyyy-MM-dd hh:mm:ss");//模式和默认语言环境的日期格式
        //System.out.println(DateUtils.convertDateToString(new Date()));//格式化 
        //Calendar cal = DateFormatUtil.convertDateToCalendar(new Date()); 
        //System.out.println(cal.getTime()); 
        //cal.add(Calendar.DAY_OF_WEEK, 3); 
        //System.out.println(cal.getTime()); 
        //System.out.println(DateDiff(convertStringToDate("2012-09-21"), convertStringToDate("2011-09-21")));
    	System.out.println(getToday());
    } 
} 